﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using MasterDetailTemplate.Services;
using MasterDetailTemplate.Services.Implementations;

namespace MasterDetailTemplate.ViewModels {
    /// <summary>
    /// 数据同步页ViewModel
    /// </summary>
    public class DataSynchronizationPageViewModel : ViewModelBase {
        /// <summary>
        /// OneDrive 同步服务
        /// </summary>
        private ISyncService _oneDriveSyncService;

        /// <summary>
        /// Onedrive日期事件存储
        /// </summary>
        private IRemoteEventStorage _oneDriveEventStorage;

        /// <summary>
        /// Azure同步服务
        /// </summary>
        private ISyncService _azureSyncService;

        /// <summary>
        /// Azure日期事件存储
        /// </summary>
        private IRemoteEventStorage _azureEventStorage;

        /// <summary>
        /// 数据同步页ViewModel
        /// </summary>
        /// <param name="oneDriveEventStorage">事件存储</param>
        /// <param name="localEventStorage">本地事件存储</param>
        /// <param name="preferenceStorage">偏好存储</param>
        public DataSynchronizationPageViewModel(
            OneDriveEventStorage oneDriveEventStorage,
            IEventStorage localEventStorage,
            IPreferenceStorage preferenceStorage) {
            _oneDriveEventStorage = oneDriveEventStorage;
            _oneDriveSyncService = new SyncService(localEventStorage,
                oneDriveEventStorage, preferenceStorage);

            _oneDriveSyncService.StatusChanged += (sender, args) =>
                OneDriveStatus = _oneDriveSyncService.Status;
            _oneDriveEventStorage.StatusChanged += (sender, args) =>
                OneDriveStatus = _oneDriveEventStorage.Status;
        }

        /// <summary>
        /// 登录状态
        /// </summary>
        public string OneDriveStatus {
            get => _oneDriveStatus;
            set => Set(nameof(OneDriveStatus), ref _oneDriveStatus, value);
        }

        private string _oneDriveStatus;

        /// <summary>
        /// 登录
        /// </summary>
        public bool OneDriveSignedIn {
            get => _oneDriveSignedIn;
            set => Set(nameof(OneDriveSignedIn), ref _oneDriveSignedIn, value);
        }

        private bool _oneDriveSignedIn;

        /// <summary>
        /// 加载
        /// </summary>
        public bool OneDriveLoading {
            get => _oneDriveLoading;
            set => Set(nameof(OneDriveLoading), ref _oneDriveLoading, value);
        }

        private bool _oneDriveLoading;




        private RelayCommand _pageAppearingCommand;

        /// <summary>
        /// 页面显示命令
        /// </summary>
        public RelayCommand PageAppearingCommand =>
            _pageAppearingCommand ?? (_pageAppearingCommand =
                new RelayCommand(async () =>
                    await PageAppearingCommandFunction()));

        private async Task PageAppearingCommandFunction() {
            OneDriveLoading = true;
            OneDriveSignedIn = await _oneDriveEventStorage.IsSignedInAsync();
            OneDriveLoading = false;
        }

        private RelayCommand _oneDriveSignInCommand;

        /// <summary>
        /// 登录Command
        /// </summary>
        public RelayCommand OneDriveSignInCommand =>
            _oneDriveSignInCommand ?? (_oneDriveSignInCommand =
                new RelayCommand(async () =>
                    await OneDriveSignInCommandFunction()));

        private async Task OneDriveSignInCommandFunction() {
            OneDriveLoading = true;
            OneDriveSignedIn = await _oneDriveEventStorage.SignInAsync();
            OneDriveLoading = false;
        }

        private RelayCommand _oneDriveSignOutCommand;
        
        /// <summary>
        /// 注销Command
        /// </summary>
        public RelayCommand OnedriveSignOutCommand =>
            _oneDriveSignOutCommand ?? (_oneDriveSignOutCommand =
                new RelayCommand(async () =>
                    await OnedriveSignOutCommandFunction()));

        private async Task OnedriveSignOutCommandFunction() {
            OneDriveLoading = true;
            await _oneDriveEventStorage.SignOutAsync();
            OneDriveSignedIn = false;
            OneDriveLoading = false;
        }

        private RelayCommand _oneDriveSyncCommand;

        /// <summary>
        /// 同步command
        /// </summary>
        public RelayCommand OneDriveSyncCommand =>
            _oneDriveSyncCommand ?? (_oneDriveSyncCommand =
                new RelayCommand(
                    async () => await OneDriveSyncCommandFunction()));

        private async Task OneDriveSyncCommandFunction() {
            OneDriveLoading = true;
            await _oneDriveSyncService.SyncAsync();
            OneDriveLoading = false;
        }
    }
}